import random

from qcloudsms_py import SmsSingleSender
from . import settings
from utils.logging import logger

sender = SmsSingleSender(settings.APP_ID, settings.APP_KEY)

# 发送验证码函数（需要提供手机号，验证码和过期时间）

# def send_msg(mobile, code, exp):
def send_msg(mobile, code):
    try:
        response =  sender.send_with_param(
            86,
            mobile,
            settings.TEMPLATE_ID,
            # params=(code, exp),
            params=[code,],
            sign=settings.SMS_SIGN,
            extend="", ext=""
        )
        print(response, response.get('result'))
        if response and response.get('result') == 0:
            return True
        msg = response.get('result')
    except Exception as msg:
        pass

    logger.error(f'短信发送失败{msg}')
    return False

def get_code():
    code = ''
    for i in range(4):
        code += str(random.randint(0, 9))
    return code